package com.house.keeper.service.impl;

import com.house.keeper.entity.CommunityPerson;
import com.house.keeper.entity.vo.CommunityPersonVo;
import com.house.keeper.mapper.CommunityPersonMapper;
import com.house.keeper.service.CommunityPersonService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.keeper.base.Result;
import com.house.keeper.base.ResultCode;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zjj
 * @date 2022-06-01 08:42:42
 *
 * @description 
 */
@Service
public class CommunityPersonServiceImpl implements CommunityPersonService {
    @Autowired
    private CommunityPersonMapper communityPersonMapper;

    /**
     * 分页查询
     *
     * @param communityPersonVo 查询条件
     * @return
     */
    @Override
    public Result selectByPage(CommunityPersonVo communityPersonVo) {
        PageHelper.startPage(communityPersonVo.getPage(),communityPersonVo.getLimit());

        QueryWrapper<CommunityPerson> wrapper = new QueryWrapper<>();


        List<CommunityPerson> communityPersonList = communityPersonMapper.selectList(wrapper);

        PageInfo<CommunityPerson> pageInfo = new PageInfo<>(communityPersonList);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Result delete(Integer id) {
        int row = communityPersonMapper.deleteById(id);
        if(row<=0){
            return Result.failure(ResultCode.PARAM_IS_INVALID);
        }
        return Result.success();
    }

    /**
     * 添加或修改
     *
     *  回滚：一般抛出的是RuntimeExcept异常
     *  在spring框架下，所有的异常都被org.springframework重写为RuntimeExcept
     * @param communityPerson
     * @return
     */
    @Override
    @Transactional
    public Result update(CommunityPerson communityPerson) {
        if(communityPerson.getId()!=null){

            int row=communityPersonMapper.updateById(communityPerson);
            if(row<=0){
                return Result.failure(ResultCode.PARAM_IS_INVALID);
            }
        }else{
//            添加
            int row =communityPersonMapper.insert(communityPerson);
            if(row<=0){
                return Result.failure(ResultCode.PARAM_IS_INVALID);
            }
        }
        return Result.success();
    }

}

